<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="需求名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入需求名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线条" prop="line">
        <el-select v-model="queryParams.line" placeholder="线条" clearable>
          <el-option
            v-for="dict in dict.type.project_line"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="需求状态" clearable>
          <el-option
            v-for="dict in dict.type.project_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:demand:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-if="false"
          v-hasPermi="['project:demand:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:demand:remove']"
          v-if="false"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:demand:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="demandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id" width="50"/>
      <el-table-column label="需求名" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="线条" align="center" prop="line" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_line" :value="scope.row.line"/>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="state" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="clickNum" width="60"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="checkerName" width="80"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="handleCheck(scope.row)"
            v-if="scope.row.state == 0"
          >送审
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="demandDetailClick(scope.row)"
          >查看详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handSeeCheck(scope.row)"
            v-if="scope.row.state != 0"
          >查看审核详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.state == 0"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.state == 0 || scope.row.state == 2 || scope.row.state == 3"
          >删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="需求名" prop="name">
          <el-input v-model="form.name" placeholder="请输入需求名"/>
        </el-form-item>
        <el-form-item label="线条" prop="line" label-width="80px">
          <el-select v-model="form.line" placeholder="线条">
            <el-option
              v-for="dict in dict.type.project_line"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-col :span="24">
          <el-form-item label="内容">
            <editor v-model="form.description" :min-height="192"/>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="detailOpen"
      width="780px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="需求名:">{{ form.name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="线条:">
              {{ form.line }} / {{ lineFormat(form) }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="创建时间:">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人1:">{{ form.checker1Name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人2:">{{ form.checker2Name }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容:">
              <div v-html="form.description"></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="seeCheckOpen" width="780px" append-to-body>
      <el-steps :active="activeStep">
        <el-step  :title="stepTitle(1)" />
        <el-step  :title="stepTitle(2)" />
        <el-step  :title="stepTitle(3)" />
        <el-step v-if="state!==3" :title="stepTitle(4)" />
      </el-steps>
      <el-form ref="checkForm" :model="checkForm" label-width="100px">
        <el-row style="margin-top: 20px">
          <el-col :span="12">
            <span >审核人： 胡昊，吴兆发</span>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listDemand, getDemand, delDemand, addDemand, updateDemand} from "@/api/project/demand";
import {addCheckProject, addClaimNeedCheck, seeCheck} from "@/api/project/checkList";
import demandDetail from "../../demandDetail.vue";

export default {
  props: {
    state: {
      type: Number,
      default: 1
    }
  },
  name: "Demand",
  computed: {
    demandDetail() {
      return demandDetail
    },
    activeStep() {
      if (this.state === 3) {
        return 3; // Show "结束"
      }
      if (this.state >= 4) {
        return 3; // Show "需求认领审核通过"
      } else if (this.state === 3) {
        return 2; // Show "需求认领待审核"
      } else if (this.state === 2) {
        return 2; // Show "需求认领待审核"
      }
      return 1; // Default to "审核通过"
    }
  },
  dicts: ['project_state', 'project_line', 'team_name', 'check_result'],
  data() {
    return {
      state: null,
      state0: null,
      state1:null,
      state2:null,
      state3:null,
      state4:null,
      state5:null,
      state6:null,
      state7:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求表格数据
      demandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen: false,
      seeCheckOpen: false,
      seeClaimNeedDetailOpen: false,
      claimNeedDetailForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        line: null,
        prize: null,
        awardTime: null,
        userId: null,
        otherPrize: null,
        state: null,
        needNum: null,
      },
      // 默认排序
      defaultSort: {prop: 'operTime', order: 'descending'},
      // 表单参数
      form: {},
      checkForm: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "课题名称不能为空", trigger: "blur"},
          {min: 2, max: 40, message: '课题名称长度必须介于 2 和 40 之间', trigger: 'blur'}
        ],
        line: [
          {required: true, message: "线条不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "描述不能为空", trigger: "blur"}
        ]
      }
    };
  },

  created() {
    this.getList();
  },

  methods: {
    stepTitle(stepIndex) {
      if (this.state === 3) {
        if (stepIndex === 1) return '待审核';
        if (stepIndex === 2) return '审核不通过';
        if (stepIndex === 3) return '结束';
        return '';
      }
      if (this.state === 6 && stepIndex === 4) {
        return '需求认领审核不通过';
      }
      switch(stepIndex) {
        case 1: return '待审核';
        case 2: return '审核通过';
        case 3: return '需求认领待审核';
        case 4: return '需求认领审核通过';
        default: return '';
      }
    },


    // 线条字典类型翻译
    lineFormat(row, column) {
      return this.selectDictLabel(this.dict.type.project_line, row.line);
    },
    // 操作人1
    check1Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker1Id);
    },    // 操作人1
    check11Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker1);
    },
    // 操作人2
    check2Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker2Id);
    },
    // 操作人2
    check22Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker2);
    },
    result1Format(row, column) {
      return this.selectDictLabel(this.dict.type.check_result, row.checkResult);
    },
    result2Format(row, column) {
      return this.selectDictLabel(this.dict.type.check_result, row.result2);
    },

    /** 查询需求列表 */
    getList() {
      this.loading = true;
      listDemand(this.queryParams).then(response => {
        this.demandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.detailOpen = false;
      this.seeCheckOpen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.checkForm = {
        checkerName: undefined,
        checkResult:null,
        checkTime:null,
        result1: null,
        result2: null,
        suggestion1: null,
        suggestion2: null,
        checker_time1: null,
        checker_time2: null,
        state: null,
      };
      this.form = {
        id: null,
        name: null,
        type: null,
        description: null,
        line: null,
        keyTask1: null,
        keyTask2: null,
        keyTask3: null,
        prize: null,
        awardTime: null,
        file: null,
        innovation: null,
        userId: null,
        clickNum: null,
        otherPrize: null,
        checker1: null,
        checker2: null,
        state: null,
        needNum: null,
        isShow: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求";
    },
    demandDetailClick(row) {
      this.$router.push({path : '/demandDetail',query:{"id" : row.id ,"from" : 2}});
    },


    /** 查看审核详情 */
    handSeeCheck(row) {
      this.reset();
      const id = row.id || this.ids
      seeCheck(id).then(response => {
        this.checkForm = response.data;
        this.state = row.state;
        this.seeCheckOpen = true;
        this.title = "审核详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDemand(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改需求";
      });
    },
    /** 送审  */
    handleCheck(row) {
      this.$modal.confirm('是否确认送审').then(function () {
        return addCheckProject(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("送审成功");
      }).catch(() => {
      });
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDemand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDemand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除需求编号为"' + ids + '"的数据项？').then(function () {
        return delDemand(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/demand/export', {
        ...this.queryParams
      }, `demand_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

